package cn.openea.perpartner.common.core.enums;

import cn.hutool.core.util.StrUtil;

import java.util.Arrays;
import java.util.List;

/**
 * @author zane_ysk@qq.com
 */

public enum ResourceType {
    /**资源枚举类型*/
    IMG(Arrays.asList(".img",".image",".png",".jpeg",".gif",".jpg"),(short)1,"图片"),
    VOICE(Arrays.asList(".mp3"),(short)2,"音频"),
    VIDEO(Arrays.asList(".mp4",".flv",".m3u8",".3gp"),(short)3,"视频"),
    DOC(Arrays.asList(".doc",".docx"),(short)4,"文档"),
    PDF(Arrays.asList(".pdf"),(short)5,"PDF"),
    PPT(Arrays.asList(".ppt",".pptx"),(short)6,"PPT"),
    RAR(Arrays.asList(".rar",".tar",".zip"),(short)7,"压缩包"),
    XML(Arrays.asList(".xml",".xlsx"),(short)8,"表格"),
    HTML(Arrays.asList(".html",".xlsx"),(short)9,"HTML"),
    CSS(Arrays.asList(".css"),(short)10,"CSS"),
    JS(Arrays.asList(".js"),(short)11,"JS"),
    LIVE(Arrays.asList(""),(short)12,"直播"),
    TXT(Arrays.asList(".text",".txt"),(short)13,"TXT"),
    Other(null,(short)99,"其他资源类型"),
    ;
    /**后缀名集合*/
    private List<String> suffix;
    /**类别*/
    private Short type;
    /**描述*/
    private String desc;

    ResourceType(List<String> suffix, Short type, String desc) {
        this.suffix = suffix;
        this.type = type;
        this.desc = desc;
    }

    public Short getType() {
        return type;
    }

    public List<String> getSuffix() {
        return suffix;
    }

    public String getDesc() {
        return desc;
    }

    public static ResourceType getByType(final Short type) {
        if(null == type) return ResourceType.Other;

        return Arrays.stream(ResourceType.values())
                .filter(item -> item.getType().equals(type))
                .findFirst()
                .orElse(ResourceType.Other);
    }

    public static ResourceType getBySuffix(String suffix){
        if(StrUtil.isBlank(suffix)){
            return ResourceType.Other;
        }
        for(ResourceType resourceType : ResourceType.values()){
            if (resourceType.getType() == 0 || resourceType.getSuffix() == null) {
                continue;
            }
            if(resourceType.getSuffix().contains(suffix.toLowerCase())){
                return resourceType;
            }
        }
        return ResourceType.Other;
    }

    public static boolean check(ResourceType resourceType,Short type){
        if(resourceType==null||type==null){return false;}
        return resourceType.type.equals(type);
    }

    public static boolean checkIn(ResourceType resourceType,ResourceType... resourceTypes){
        return Arrays.stream(resourceTypes).anyMatch(item -> item == resourceType);
    }
}
